package com.nsy.shortlink.admin.remote;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nsy.shortlink.admin.common.convention.result.Result;
import com.nsy.shortlink.admin.remote.dto.req.RecycleBinSaveReqDTO;
import com.nsy.shortlink.admin.remote.dto.req.ShortLinkCreateReqDTO;
import com.nsy.shortlink.admin.remote.dto.req.ShortLinkPageReqDTO;
import com.nsy.shortlink.admin.remote.dto.req.ShortLinkUpdateReqDTO;
import com.nsy.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.nsy.shortlink.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.nsy.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public interface ShortLinkRemoteService {

    static Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam) {

        String result = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));

        return JSON.parseObject(result, new TypeReference<>() {
        });
    }

    static Result<IPage<ShortLinkPageRespDTO>> pageShortlink(ShortLinkPageReqDTO requestParam) {
        Map<String,Object> map = new HashMap<>();
        map.put("gid",requestParam.getGid());
        map.put("current",requestParam.getCurrent());
        map.put("size",requestParam.getSize());
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/admin/v1/page", map);

        //result是null的话会报错
        return JSON.parseObject(result, new TypeReference<>() {
        });
    }

    static Result<List<ShortLinkGroupCountQueryRespDTO>> listShortLinkGroupCount(List<String> requestParam){
        Map<String,Object> requestMap =new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(result, new TypeReference<Result<List<ShortLinkGroupCountQueryRespDTO>>>() {});
    }

    static    void updateShortLink(ShortLinkUpdateReqDTO requestParam){
        String result = HttpUtil.post("http://127.0.0.1:8001/api/short-link/admin/v1/create", JSON.toJSONString(requestParam));
    }

    /**
     * 根据 URL 获取标题
     *
     * @param url 目标网站地址
     * @return 网站标题
     */
    static  Result<String>   getTitleByUrl(String url){

        Map<String,Object> requestMap =new HashMap<>();
        requestMap.put("url",url);
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title", requestMap);
        return JSON.parseObject(result, new TypeReference<Result<String>>() {});
    }

    static Result<Void> saveRecycleBin(RecycleBinSaveReqDTO requestParam){
        String result = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
        return JSON.parseObject(result, new TypeReference<Result<Void>>() {});

    }


}
